//
//  AllPicViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/28.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "AllPicViewController.h"
#import "UIImageView+WebCache.h"
#import "InterFace.h"
#import "UIViewAdditions.h"

@interface AllPicViewController ()<UIGestureRecognizerDelegate,UIAlertViewDelegate>
{
    UIImageView *imge;
}
@end

@implementation AllPicViewController
@synthesize imgName,zHeight,zWidth;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    imge = [[UIImageView alloc] initWithFrame:CGRectMake(0, (SCREEN_HEIGHT-SCREEN_WIDTH/zWidth*zHeight)/2,SCREEN_WIDTH,SCREEN_WIDTH/zWidth*zHeight)];
    [imge sd_setImageWithURL:[NSURL URLWithString:self.imgName] placeholderImage:[UIImage imageNamed:@"loading"]];
    [self.view addSubview:imge];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
    
    UILongPressGestureRecognizer *longPressReger = [[UILongPressGestureRecognizer alloc]
                                                    
                                                    initWithTarget:self action:@selector(handleLongPress:)];
    
    longPressReger.minimumPressDuration = 0.5;
    
    [self.view addGestureRecognizer:longPressReger];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
    //长按手势默认会执行两次 一个开始一个结束，要做判断
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"确定保存么?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    alert.tag = 240;
    [alert show];
        
    }
}
#pragma mark - UIAlertView  delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 240) {
        UIImageWriteToSavedPhotosAlbum(imge.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    }
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"保存图片至相册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 234;
        [alert show];
    }else
    {
        message = [error description];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"保存图片至相册失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 233;
        [alert show];

    }
    NSLog(@"message is %@",message);
}
-(void)singleTap{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewDidDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
